package com.linkage.educloud.ah.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;

import com.linkage.educloud.ah.Constants;
import com.linkage.educloud.ah.MyApplication;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.adapter.AppCommonAdapter;
import com.linkage.educloud.ah.adapter.NewsHomeListAdapter;
import com.linkage.educloud.ah.bean.AccountData;
import com.linkage.educloud.ah.bean.AppBean;
import com.linkage.educloud.ah.bean.ArticleBean;
import com.linkage.educloud.ah.bean.BannerBean;
import com.linkage.educloud.ah.bean.Child;
import com.linkage.educloud.ah.bean.QuickBean;
import com.linkage.educloud.ah.config.AccountConfig;
import com.linkage.educloud.ah.mvp.contract.IHomeContract;
import com.linkage.educloud.ah.mvp.presenter.HomePresenter;
import com.linkage.educloud.ah.view.BasicFragment;
import com.linkage.educloud.ah.view.activity.classspace.DynamicListActivity;
import com.linkage.educloud.ah.view.activity.classspace.DynamicWriterTextActivity;
import com.linkage.educloud.ah.view.activity.topic.TopicGroupListActivity;
import com.linkage.educloud.ah.widget.BannerM;
import com.linkage.educloud.ah.widget.ChoosePicDialog;
import com.linkage.educloud.ah.widget.MyMore;
import com.linkage.educloud.ah.widget.MyQuick;
import com.linkage.educloud.ah.widget.NoScrollGridView;
import com.linkage.educloud.ah.widget.SwitchChildView;
import com.zzhou.common.library.util.ToastShow;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 应用首页
 * Created by Mr'Tang on 2018/9/10.
 */

public class HomeFragment extends BasicFragment implements IHomeContract.IView {

    @BindView(R.id.fragment_home_scroll)
    ScrollView fragment_home_scroll;
    @BindView(R.id.fragment_home_banner)
    BannerM fragment_home_banner;
    @BindView(R.id.fragment_home_switchchild)
    SwitchChildView fragment_home_switchchild;
    @BindView(R.id.fragment_home_quick)
    MyQuick fragment_home_quick;
    @BindView(R.id.fragment_home_app_more)
    MyMore fragment_home_app_more;
    @BindView(R.id.fragment_home_app)
    NoScrollGridView fragment_home_app;
    @BindView(R.id.fragment_home_news_more)
    MyMore fragment_home_news_more;
    @BindView(R.id.fragment_home_news)
    NoScrollGridView fragment_home_news;
    @BindView(R.id.fragment_home_zhitongche)
    ImageView fragment_home_zhitongche;

    IHomeContract.IPresenter iPresenter;
    private AccountData accountData;

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_home;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        iPresenter = new HomePresenter(getActivity(), this);
        initView(iPresenter);
        accountData = AccountConfig.getLoginAccount();
        iPresenter.getRollAds(accountData.getUserType());
        initChild(MyApplication.getInstance().getResovler().getChild(accountData.getLoginAccount()), accountData);
        initQuick();
        MoreClick();
        iPresenter.getMyApps(1);
//        iPresenter.getArticle(2, 1);
    }

    //加载热门应用信息
    public void initApps(List<AppBean> list) {
        list = list.subList(0, 4);
        AppCommonAdapter adapter = new AppCommonAdapter(getActivity(), list);
        fragment_home_app.setAdapter(adapter);
    }

    //加载精彩资讯信息
    public void initNews(List<ArticleBean> list) {
        NewsHomeListAdapter adapter = new NewsHomeListAdapter(getActivity(), list);
        fragment_home_news.setAdapter(adapter);
    }

    //更多栏点击事件
    private void MoreClick() {
        fragment_home_app_more.setMoreClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), DynamicListActivity.class);
                startActivity(intent);
            }
        });
        fragment_home_news_more.setMoreClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TopicGroupListActivity.launch(getActivity());
            }
        });
    }

    //初始化快捷区域
    private void initQuick() {
        List<QuickBean> quickBeans = new ArrayList<>();
        for (int a = 0; a < 38; a++) {
            QuickBean quickBean = new QuickBean();
            quickBean.setName("应用" + a);
            quickBean.setDesc("描述" + a);
            quickBean.setImg("");
            quickBeans.add(quickBean);
        }
        fragment_home_quick.setData(quickBeans);
        fragment_home_quick.show();
    }

    //初始化孩子头像区域
    private void initChild(List<Child> lists, AccountData accountData) {

        if (accountData.getUserType() == 1) {
            fragment_home_switchchild.setVisibility(View.GONE);
        } else {
            fragment_home_switchchild.setVisibility(View.VISIBLE);
            if (lists != null && lists.size() != 0) {
                fragment_home_switchchild.setData(lists);
                fragment_home_switchchild.setAlbumClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        fragment_home_switchchild.getDialog().dismiss();
                        getActivity().startActivityForResult(ChoosePicDialog.gotoAlbum(), Constants.PIC.PIC_ALBUM);
                    }
                });
                fragment_home_switchchild.setShotClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        fragment_home_switchchild.getDialog().dismiss();
                        getActivity().startActivityForResult(ChoosePicDialog.gotoShot("xiaoma.jpg"), Constants.PIC.PIC_Shot);
                    }
                });
            }
        }
    }

    //选择照片回调相关
    @Override
    public void onActivityResult(int requestCode, int resultCode,
                                 final Intent data) {
        switch (requestCode) {
            case Constants.PIC.PIC_ALBUM://相册选择回调
                if (data != null) {
                    getActivity().startActivityForResult(ChoosePicDialog.startPhotoZoom(data.getData(), ""), Constants.PIC.PIC_OK);
                }
                break;
            case Constants.PIC.PIC_Shot://拍摄界面回调
                getActivity().startActivityForResult(ChoosePicDialog.startPhotoZoom(null, "/xiaoma.jpg"), Constants.PIC.PIC_OK);
                break;
            case Constants.PIC.PIC_OK://裁剪界面回调
                if (data != null) {
//                    Bundle extras = data.getExtras();
//                    Bitmap bitmap = extras.getParcelable("data");
//                    iv_image.setImageBitmap(bitmap);
                }
                break;
        }
    }

    //加载banner数据
    public void showBanner(final List<BannerBean> list) {
        fragment_home_banner.setBannerBeanList(list);
        fragment_home_banner.setOnItemClickListener(new BannerM.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                ToastShow.show(getActivity(), "轮显详情界面待添加");
            }
        });
        fragment_home_banner.show();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}